package vashion.azeroth.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.core.manager.personal.PersonalExperienceManager;
import vashion.azeroth.core.manager.personal.PersonalMemberVipManager;
import vashion.azeroth.core.personal.pojo.PersonalExperience;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.personal.pojo.PersonalMemberVip;
import vashion.azeroth.core.service.OperateExperienceService;

/**
 * 计算baseobject 
 * 
 * */
@Service
public class OperateExperienceServiceImpl implements OperateExperienceService<Object> {

	private final double higherWeight = 0.05;//上线加权比重
	private final double lowerWeight = 0.01;//下线加权后比重
	private final double loginWeight = 27; // 登录权重(经验值减少权重) -- 27点每天x没有登录的天数

//	@Autowired
//	private OperateExperienceService operateExperienceServices;
	
	@Autowired
	private PersonalMemberVipManager personalMemberVipManager;
	
	@Autowired
	private PersonalExperienceManager personalExperienceManager;

	/**
	 * 计算我的上线影响到我的实际经验值 计算我的下线影响到我的实际经验值 计算我的经验值 每个用户只能有一个上线，可以有N个下线，so
	 * 对于上线使用object存储，对于下线用object[]存储
	 * 
	 * 本方法用于获取用户上、下线加权后经验值
	 * 
	 * */
	
	@Override
	public double getOperateMyExperience(PersonalMember higher,
			PersonalMember[] lowers, int myBaseExp) {
		double sumLowersExp = 0;
		double higherExp = 0;
		double myOpeExp = 0;

		if (higher == null) {
			if (lowers.length == 0 || lowers == null) {
				return myBaseExp;
			} else {

				sumLowersExp = getSumLowersExperience(lowers);
			}
		} else if (null == lowers || lowers.length == 0) {
			higherExp = getHigherExperience(higher);
		} else {
			sumLowersExp = getSumLowersExperience(lowers);
			higherExp = getHigherExperience(higher);
		}
		myOpeExp = myBaseExp + higherExp + sumLowersExp;
		return myOpeExp;
	}

	/**
	 * 获取user上线加权后的经验值
	 * 
	 * */
	public double getHigherExperience(PersonalMember higher) {
		double higherExp = 0;
		
		PersonalMemberVip pmv = personalMemberVipManager.get(higher.getMemberId());
		if (null == pmv) {
			pmv = new PersonalMemberVip();
			pmv.setVipLevel("0");
		}
		PersonalExperience experience = new PersonalExperience();
		experience.setMemberId(higher.getMemberId());
		PersonalExperience higherExperience = personalExperienceManager.getExperienceByMemberId(experience);
		if (null == higherExperience) {
			higherExperience = new PersonalExperience(); 
			higherExperience.setValueBase(0);
		}
		int level = Integer.valueOf(pmv.getVipLevel());
		int higherBaseExp = higherExperience.getValueBase() * level;
		higherExp = higherBaseExp * higherWeight;
		return higherExp;
	}

	/**
	 * 获取user下线加权后的经验值之和
	 * 
	 * */
	public double getSumLowersExperience(PersonalMember[] lowers) {
		double sumLowerBaseExps = 0;
		double sumLowersExp = 0;
		
		for (int i = 0; i < lowers.length; i++) {
			int level = 0;
			if (lowers[i] == null) {
				continue;// 传入下线数组如果出现null，则继续
			} else {
				PersonalMemberVip pmv = personalMemberVipManager.get(lowers[i].getMemberId());
				
				if (null == pmv) {
					continue;
				}else {
					level = Integer.valueOf(pmv.getVipLevel());
				}
				
				PersonalExperience experience = new PersonalExperience();
				experience.setMemberId(lowers[i].getMemberId());
				PersonalExperience lowerExp = personalExperienceManager.getExperienceByMemberId(experience);
				int lowerBaseExp = lowerExp.getValueBase();
				sumLowerBaseExps += lowerBaseExp * level;
			}
		}
		double countWeight = lowers.length / 10.0 + 1.0 - 0.1; // 下线数量系数和下线个数有关，每增加一个，加0.1，从0开始所以要减0.1
		System.out.println(countWeight);
		sumLowersExp = sumLowerBaseExps * lowerWeight * countWeight;
		return sumLowersExp;
	}

	public double getMyBaseExp(String uname) {
		double currentBaseExp = getCurrentExp();
		double myBaseExp = 0.0;
		long current = System.currentTimeMillis();
		double days = current - getLastLoginTime() / 86400.0; // 非按整点计，按满足24小时计
		myBaseExp = currentBaseExp - days * loginWeight; // 经验值的累加由订单、分享、被点赞、被转发action被触发时即时更新，不在此处累加
		return myBaseExp;
	}

	public long getLastLoginTime() {
		// 需要获取最后登录时间
		return 0;
	}

	public double getCurrentExp() {
		// 需要获取当前DB中最新的经验值；
//		double resCount = 0;
		
		return 0;
	}
//
//	public static void main(String[] args) {
//		OperateExperienceServiceImpl test = new OperateExperienceServiceImpl();
//		UserExperience higher = new UserExperience();
//		// higher.setBaseExp(3000);
//		// higher.setLevel(3);
//		UserExperience[] lowers = new UserExperience[4];
//		//
//		UserExperience lower0 = new UserExperience();
//		// lower0.setBaseExp(3400);
//		// lower0.setLevel(4);
//		lowers[0] = lower0;
//		//
//		// UserExperience lower1 = new UserExperience();
//		// lower1.setBaseExp(6000);
//		// lower1.setLevel(5);
//		// lowers[1] = lower1;
//		//
//		UserExperience lower2 = new UserExperience();
//		lower2.setBaseExp(2000);
////		lower2.setLevel(2);
//		lowers[2] = lower2;
//		//
//		UserExperience lower3 = new UserExperience();
//		// lower3.setBaseExp(3300);
//		// lower3.setLevel(3);
//		lowers[3] = lower3;
//
////		double myExp = test.getOperateMyExperience(higher, lowers, 5000);
////		System.out.println(myExp);
//	}

}
